package com.bizdk.fastfx.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bizdk.fastfx.domain.entity.Doctor;
import com.bizdk.fastfx.domain.mapper.DoctorMapper;
import com.bizdk.fastfx.model.DoctorModel;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class DoctorService extends ServiceImpl<DoctorMapper, Doctor> {

    public ObservableList<DoctorModel> listModel() {
        List<Doctor> list = this.list();
        if (null == list || list.isEmpty()) {
            return FXCollections.observableArrayList();
        }

        List<DoctorModel> collect = list.stream().map(DoctorModel::new).collect(Collectors.toList());
        return FXCollections.observableArrayList(collect);
    }

    public void addModel(DoctorModel model) {
        this.save(model.toDoctor());
    }

    public void updateModel(DoctorModel model) {
        this.updateById(model.toDoctor());
    }

    public void delModel(DoctorModel model) {
        this.removeById(model.getId());
    }

    public Doctor findOne(String username, String password) {
        LambdaQueryWrapper<Doctor> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Doctor::getName, username).eq(Doctor::getPassword, password);
        return getOne(wrapper);
    }
}
